package com.haochang.chunkvod.controller.fragment.hostess;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseFragment;
import com.haochang.base.BaseGridView;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.other.GridViewUtil;

import java.util.ArrayList;

/**
 *         created at 2016/8/27 17:03
 *         转台页面
 */
public class DeskSubFragment extends BaseFragment
{

	private BaseListView listview_desk_sub_rooms;//桌台各种转台
	private ArrayList<HostessInfo> subInfos = new ArrayList<HostessInfo>();
	private QuickAdapter<HostessInfo> adapter;
	private String currentDesk = "";
	private HostessData hostessData;
	private String billId;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_desk_sub, container, false);
		initView(view);
		return view;
	}

	private void initView(View view)
	{

		hostessData = new HostessData((BaseActivity) getActivity());
		Button desk_sub_ok = (Button) view.findViewById(R.id.desk_sub_ok);//确定按钮
		listview_desk_sub_rooms = (BaseListView) view.findViewById(R.id.listview_desk_sub_rooms);
		desk_sub_ok.setOnClickListener(baseClickListener);
		getData();
		billId = getArguments().getString(IntentKey.BILL_ID);
		//		roomStatus = getArguments().getString(IntentKey.ROOM_STATUS);
	}

	/**
	 * 获取数据并显示
	 */
	private void getData()
	{
		hostessData.requestEmptyDeskList(new HostessData.DeskListListener()
		{
			@Override
			public void onRequestSuccess(ArrayList<HostessInfo> list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					subInfos.clear();
					subInfos.addAll(list);
					showAllDesks();
				}
			}

			@Override
			public void onFinish()
			{

			}

		});

	}

	/**
	 * 显示所有空桌台
	 */
	private void showAllDesks()
	{
		adapter = new QuickAdapter<HostessInfo>(getActivity(), R.layout.item_sub_list, subInfos)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, HostessInfo item)
			{
				helper.setText(R.id.tv_title, item.getRoomType());
				BaseGridView grid = helper.getView(R.id.grid_sub_item);
				QuickAdapter gridAdapter = new QuickAdapter<HostessInfo>(getActivity(), R.layout.item_part_modify_grid_layout, item.getRooms())
				{
					@Override
					protected void convert(BaseAdapterHelper helper, final HostessInfo item)
					{
						helper.getView(R.id.img_del).setVisibility(View.GONE);
						helper.setText(R.id.tv_partName, item.getRoomNum());
						if (!TextUtils.isEmpty(currentDesk) && currentDesk.equals(item.getRoomId()))
						{
							helper.getView(R.id.tv_partName).setBackgroundResource(R.drawable.btn_red);
							((BaseTextView) helper.getView(R.id.tv_partName)).setTextColor(getResources().getColor(R.color.white));
						} else
						{
							helper.getView(R.id.tv_partName).setBackgroundResource(R.drawable.btn1);
							((BaseTextView) helper.getView(R.id.tv_partName)).setTextColor(getResources().getColor(R.color.color_282828));
						}

						helper.getView(R.id.tv_partName).setOnClickListener(new OnBaseClickListener()
						{
							@Override
							public void onBaseClick(View v)
							{
								currentDesk = item.getRoomId();
								adapter.replaceAll(subInfos);
							}
						});
					}
				};
				grid.setAdapter(gridAdapter);
				GridViewUtil.setListViewHeightBasedOnChildren(getActivity(), grid);
				gridAdapter.notifyDataSetChanged();
			}
		};
		listview_desk_sub_rooms.setAdapter(adapter);
	}

	private OnBaseClickListener baseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.desk_sub_ok:
				//转台 提交数据
				if (!TextUtils.isEmpty(billId))
				{
					if (TextUtils.isEmpty(currentDesk))
					{
						ToastUtils.showText(getActivity(), "请选择桌台");
					} else
					{
						hostessData.requestSubDesk(billId, currentDesk, new HostessData.NoticeListener()
						{
							@Override
							public void onRequestSuccess(String notice)
							{
								if (!TextUtils.isEmpty(notice))
								{
									ToastUtils.showText(getActivity(), "转台成功");
									getActivity().finish();
								}
							}

							@Override
							public void onFail(int errno, String errMsg)
							{

							}
						});
					}
				}
				break;
			}
		}
	};

}
